<template>
  <div class="app-container">
    <div class="funOperate">
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item label="管理员登录账号:" prop="username">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入管理员登录账号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="时间:">
          <el-date-picker
            v-model="dateRange"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd"
            >新增</el-button
          >
        </el-col>
      </el-row>
    </div>

    <el-table
      v-loading="loading"
      :data="adminList"
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="管理员ID" align="center" prop="id" />
      <el-table-column label="管理员登录账号" align="center" prop="username" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="登录时间" align="center" prop="loginTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />
    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="管理员账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入管理员账号" />
        </el-form-item>
        <el-form-item label="管理员密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入管理员密码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAdmin,
  getAdmin,
  addAdmin,
  updateAdmin,
  deleteAdmin
} from '@/api/admin'
export default {
  name: "Admin",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 管理员表表格数据
      adminList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        name: null,
        startTime: null,
        endTime: null,
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "管理员名称不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询管理员表列表
    getList() {
      this.loading = true;
      if (this.dateRange) {
        this.queryParams.startTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
      listAdmin(this.queryParams).then((response) => {
        this.adminList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userName: null,
        status: "0",
        createTime: null,
        updateTime: null,
      };
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.dateRange = [];
      this.queryParams.name = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 新增按钮操作
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加管理员信息";
    },
    // 修改按钮操作
    handleUpdate(row) {
      this.reset();
      getAdmin(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改管理员信息";
      });
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate((valid) => {
        console.log(1111);
        if (valid) {
          if (this.form.id != null) {
            updateAdmin(this.form).then((response) => {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.open = false;
              this.getList();
            });
          } else {
            addAdmin(this.form).then((response) => {
              this.$message({
                message: "新增成功",
                type: "success",
              });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 删除按钮操作
    handleDelete(row) {
      const id = row.id;
      this.$confirm(
        '是否确认删除用户信息编号为"' + id + '"的数据项吗？',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          return deleteAdmin(id);
        })
        .then(() => {
          this.getList();
          this.$message({
            message: "删除成功",
            type: "success",
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  padding: 20px;
}
.funOperate {
  display: flex;
  justify-content: space-between;
}
</style>
